import ToolRequest from '@/base/newToolAxios';
import RequestOption from '@/base/RequestOption';
import { getActToolUrl } from '@/base/constant';
import { c } from 'vite/dist/node/types.d-aGj9QkWt';
const tr = new ToolRequest();

export default class UserDao {
  /**
   * 查询用户。
   *
   * @param  userId   用户ID
   * @param userName 用户名
   * @param phone    手机号
   * @param projectType    项目类型
   * @param userType 用户类型
   *
   * @return 查找到的用户列表。
   */
  async findUser(
    userId: number,
    userName: string,
    phone: string,
    projectType: number,
    userType: number
  ) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findUser',
        userId: userId,
        userName: userName,
        phone: phone,
        projectType: projectType,
        userType: userType
      },
      ro
    );
  }
  async findClassStudents(
    classId: number,
    teamId: number,
    studentId: number,
    userName: string,
    haveTeam: boolean,
    currentPage: number,
    pageSize: number
  ) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findClassStudents',
        classId: classId,
        teamId: teamId,
        studentId: studentId,
        userName: userName,
        haveTeam: haveTeam,
        currentPage: currentPage,
        pageSize: pageSize
      },
      ro
    );
  }

  async setTeamForStudents(classId: number, teamId: number, studentIds: Array<number>) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'setTeamForStudents',
        classId: classId,
        teamId: teamId,
        studentIds: studentIds.join(',')
      },
      ro
    );
  }

  async findAllStudents(
    classId: number,
    studentId: number,
    studentName: string,
    current: number,
    pageSize: number
  ) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findAllStudents',
        classId: classId,
        studentId: studentId,
        studentName: studentName,
        currentPage: current,
        pageSize: pageSize
      },
      ro
    );
  }
  /**
   * 添加用户
   * @param userId 学号/工号
   * @param bandId 帮区ID
   * @param password 密码
   * @param email 邮箱
   * @param phone 手机号
   * @param role 用户角色 (student/teacher)
   * @param studentName 学生姓名 (当role为student时必填)
   * @param teacherName 教师姓名 (当role为teacher时必填)
   * @returns 添加结果
   */
  async addUser(
    userId: number,
    bandId: number,
    password: string,
    email: string,
    phone: string,
    role: 'student' | 'teacher',
    studentName?: string,
    teacherName?: string
  ) {
    const ro = new RequestOption();

    // 构建基础请求参数
    const payload: any = {
      action: 'addUser',
      userId,
      bandId,
      password,
      email,
      phone,
      role
    };

    // 根据角色添加特定参数并验证
    if (role === 'student') {
      if (!studentName) {
        throw new Error('学生姓名不能为空');
      }
      payload.studentName = studentName;
    } else {
      if (!teacherName) {
        throw new Error('教师姓名不能为空');
      }
      payload.teacherName = teacherName;
    }

    return await tr.post(getActToolUrl(), payload, ro);
  }

  // 添加用户查找方法
  async findUserByPassword(userId: number, password: string, role: 'student' | 'teacher') {
    const ro = new RequestOption();
    const payload = {
      action: 'findUserByPassword',
      userId,
      password,
      role
    };
    return await tr.post(getActToolUrl(), payload, ro);
  }
}
